package com.async.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.async.boot.config.SnowflakeConfig;
import com.async.boot.entity.Order;
import com.async.boot.mapper.OrderMapper;
import com.async.boot.task.AsyncTask;
import com.async.boot.task.AsyncTaskTest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.boot.common.CommonResult;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1")
@Slf4j
public class UserController {

  @Autowired
  private AsyncTask task;
  @Resource
  private OrderMapper orderMapper;
  @Resource
  private SnowflakeConfig snowflakeConfig;
  @Resource
  private AsyncTaskTest asyncTaskTest;
  List<Order> orderList = new ArrayList<>();

  @GetMapping("async_task")
  public CommonResult exeTask() throws InterruptedException {

    long begin = System.currentTimeMillis();
//        Future<String> task4 = task.task4();
    task.task1();
    task.task2();
    task.task3();
//        Future<String> task5 = task.task5();
//        Future<String> task6 = task.task6();
    //如果都执行往就可以跳出循环,isDone方法如果此任务完成，true
//        for(;;){
//            if (task4.isDone() && task5.isDone() && task6.isDone()) {
//                break;
//            }
//        }

    long end = System.currentTimeMillis();
    long total = end - begin;
    System.out.println("执行总耗时=" + total);
    return CommonResult.success(total);
  }

  @GetMapping("async_task_dr")
  public CommonResult execDr() throws InterruptedException {
    String s = "";
    for (int i = 0; i < 100; i++) {
      System.out.println("执行");
    }
//        TimeUnit.SECONDS.sleep(3);
    System.out.println("==========>>>");

    System.out.println("当前线程名称为：" + Thread.currentThread().getName());

    return CommonResult.success(s);
  }

  @PostConstruct
  public void init() {
    LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Order::getProductId, 20000904L);
    orderList = orderMapper.selectList(queryWrapper);
//        for (int i = 0; i < 10000; i++) {
//            Order order = new Order();
//            order.setId(snowflakeConfig.snowflakeId());
//            order.setProductId(RandomUtil.randomLong(100000000L));
//            order.setQuantity(RandomUtil.randomInt(100000000));
//            orderMapper.insert(order);
//        }
  }

  @GetMapping("/execute")
  public CommonResult<String> execute() {
    if (CollectionUtil.isNotEmpty(orderList)) {
      int count = orderList.size();
      for (int i = 0; i < count; i = i + 10) {
        if (i + 10 < count) {
          asyncTaskTest.execute(orderList.subList(i, i + 10));
        } else {
          asyncTaskTest.execute(orderList.subList(i, count));
        }
      }
    }
    return CommonResult.success(asyncTaskTest.getInteger() + "");
  }
}
